package com.example.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.studyapplication.R;

/**
 * @Description:
 * @Author: 葛浩杰
 * @Version: 1.0 2021/4/21
 */
public class PkView extends View {

    private final int tiltAngle = 20;//倾斜度，只代表大小，不代表角度
    private final int pkSpace = 20; //pk间隔
    private Paint paint;
    private int leftSelectColor;//左侧选中颜色
    private int leftUnSelectColor;//左侧未选中颜色
    private int rightSelectColor;//右侧选中颜色
    private int rightUnSelectColor;//右侧未选中颜色
    private int voteType;//投票类型

    private int width;
    private int halfWidth;
    private int height;

    public PkView(Context context) {
        super(context);
    }

    public PkView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PkView);
        leftSelectColor = array.getColor(R.styleable.PkView_left_select_color,0xff666666);
        leftUnSelectColor = array.getColor(R.styleable.PkView_left_unselect_color,0xff666666);
        rightSelectColor = array.getColor(R.styleable.PkView_right_select_color,0xff666666);
        rightUnSelectColor = array.getColor(R.styleable.PkView_right_unselect_color,0xff666666);
        voteType = array.getColor(R.styleable.PkView_vote_type,0xff666666);
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setPathEffect(new CornerPathEffect(10));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        halfWidth = width/2;
        height = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //绘制左侧方块
        if(voteType == 1){
            paint.setColor(leftSelectColor);
        } else {
            paint.setColor(leftUnSelectColor);
        }
        Path path1 = new Path();
        path1.moveTo(0,0);
        path1.lineTo(halfWidth + tiltAngle,0);
        path1.lineTo(halfWidth - tiltAngle - pkSpace, height);
        path1.lineTo(0, height);
        path1.close();
        canvas.drawPath(path1,paint);

        //绘制右侧方块
        if(voteType == 2){
            paint.setColor(rightSelectColor);
        } else {
            paint.setColor(rightUnSelectColor);
        }
        Path path2 = new Path();
        path2.moveTo(halfWidth + tiltAngle + pkSpace,0);
        path2.lineTo(width,0);
        path2.lineTo(width, height);
        path2.lineTo(halfWidth - tiltAngle, height);
        path2.close();
        canvas.drawPath(path2,paint);

        //绘制PK图片
//        paint.reset();
//        bitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.home_item_pk)).getBitmap();
//        canvas.drawBitmap(bitmap, halfWidth - bitmap.getWidth()/2, height/2 - bitmap.getHeight()/2,  paint);

    }

    public void setVoteType(int type){
        this.voteType = type;
        invalidate();
    }
}
